package tools.cookietools;

import lombok.Getter;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import tools.streamtools.NullableStream;

import java.util.Date;
import java.util.List;

import static java.util.stream.Collectors.toList;


public class ReadOnlyCookieStore implements CookieStore {

    @Getter
    private List<Cookie> cookies;

    private ReadOnlyCookieStore() {
        this(null);
    }

    public ReadOnlyCookieStore(List<Cookie> cookies) {
        this.cookies = NullableStream.of(cookies).collect(toList());
    }

    @Override
    public void addCookie(Cookie cookie) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean clearExpired(Date date) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    public BasicCookieStore toBasicCookieStore() {
        BasicCookieStore basicCookieStore = new BasicCookieStore();
        this.cookies.forEach(basicCookieStore::addCookie);
        return basicCookieStore;
    }
}
